const express = require('express')
const Message = require('../util/Message')
const {Subject, Class, QueryTypes, sequelize, Course, Op, Homework, HomeworkLog, User} = require('../util/sequelize')
const {formatTime, compareTime} = require('../util/formatTime')
const router = express.Router()


router.get('/getStudentCourse', async (req, res) => {
    const {class_id} = req.query
    const course = await Course.findAll({
        attributes: ['id', 'video', 'img', 'subject_id', 'class_id', 'teacher_id', ['name', 'title'], ['description', 'desc'], 'filePath'],
        where: {class_id}
    })
    res.send(Message.sendSuccess(course))
})
router.get('/getCourseHomework', async (req, res) => {
    const {course_id} = req.query
    const homework = await Homework.findAll({
        where: {course_id}
    })
    const list = JSON.parse(JSON.stringify(homework))
    list.forEach(item => {
        item.status = compareTime(new Date(item.startTime), new Date(item.endTime))
    })
    res.send(Message.sendSuccess(list.reverse()))
})
router.post('/submitHomework', async (req, res) => {
    const {answer: studentAnswer, student_id, homework_id, file} = req.body
    await HomeworkLog.create({
        studentAnswer, student_id, homework_id, file
    })
    res.send(Message.sendSuccess(null, '提交成功'))
})
router.get('/getHomeworkLog', async (req, res) => {
    const {student_id, homework_id} = req.query
    const homeworkLog = await HomeworkLog.findOne({
        where: {student_id, homework_id}
    })
    res.send(Message.sendSuccess(homeworkLog))
})

module.exports = router
